"""
FastAPI应用主入口
"""
import os
import uvicorn
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from loguru import logger

from app.config import settings
from app.database.connection import db_manager
from app.api.routes import router
from app.utils.logging import setup_logging


@asynccontextmanager
async def lifespan(app: FastAPI):
    """应用生命周期管理"""
    # 启动时执行
    logger.info("启动法律问答系统...")
    
    try:
        # 初始化数据库
        db_manager.init_database()

        # 尝试初始化Redis（失败不影响系统启动）
        try:
            db_manager.init_redis()
            logger.info("Redis初始化成功")
        except Exception as redis_error:
            logger.warning(f"Redis初始化失败，系统将在无缓存模式下运行: {redis_error}")

        # 预热模型（可选）
        logger.info("系统初始化完成")

    except Exception as e:
        logger.error(f"系统初始化失败: {e}")
        raise
    
    yield
    
    # 关闭时执行
    logger.info("关闭法律问答系统...")
    db_manager.close_connections()


# 创建FastAPI应用
app = FastAPI(
    title=settings.app_name,
    version=settings.app_version,
    description="基于RAG的法律领域问答系统",
    lifespan=lifespan
)

# 添加CORS中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 生产环境中应该限制具体域名
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册路由
app.include_router(router, prefix="/api/v1", tags=["法律问答"])

# 挂载静态文件
if os.path.exists("static"):
    app.mount("/static", StaticFiles(directory="static"), name="static")


@app.get("/")
async def root():
    """根路径 - 返回Web界面"""
    if os.path.exists("static/index.html"):
        return FileResponse("static/index.html")
    else:
        return {
            "message": "法律问答系统API",
            "version": settings.app_version,
            "docs": "/docs",
            "web_ui": "Web界面文件未找到"
        }


@app.exception_handler(Exception)
async def global_exception_handler(request, exc):
    """全局异常处理"""
    logger.error(f"未处理的异常: {exc}")
    return JSONResponse(
        status_code=500,
        content={"detail": "内部服务器错误"}
    )


if __name__ == "__main__":
    # 设置日志
    setup_logging()
    
    # 启动应用
    uvicorn.run(
        "app.main:app",
        host=settings.api_host,
        port=settings.api_port,
        workers=settings.api_workers,
        reload=settings.debug,
        log_level=settings.log_level.lower()
    )
